import pandas, duckdb
from config import ini_op
from openpyxl import  load_workbook
import os
from utils.path_utils import get_project_root
from database.duckdb.duckdb_base import get_connection


def run(filepath, timenow):
    # workbook = load_workbook(filepath, data_only=True)
    # sheet = workbook.active
    nd = ini_op.getinivalue('basic_config', 'kjnd')[0]
    df = pandas.read_excel(filepath, dtype=str)
    conn = get_connection()
    conn.register('temp_bmzd', df)
    conn.execute("""
        delete from t_api_bm where fiyear = '"""+nd+"""';
        insert into t_api_bm (
            id,
            code,
            name_chs,
            fullname_chs,
            accorgid,
            orgcode,
            orgname,
            admindeptid,
            treeinfo_isdetail,
            treeinfo_layer,
            treeinfo_path,
            parentid,
            isdisable,
            disableyear,
            sortorder,
            fiyear,
            timestamp_createdon,
            timestamp_lastchangedon,
            esun_api_update)
        select 核算部门ID,核算部门编号,核算部门名称,核算部门全称,核算组织ID,核算组织编号,核算组织名称,组织机构ID,明细否,级次,分级码,父级ID,停用否,停用年度,排序号,'"""+nd+"""',
        创建时间,最后修改时间,'"""+timenow+"""'
        from temp_bmzd;
        """)
    conn.close()


if __name__ == '__main__':
    run(r'C:\Users\lzq32\Downloads\往来单位查询.xlsx', '20250515113745')